package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.HAnnex;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.dao.HAnnexDao;
import com.zhongwang.mms.module.half.model.HInventoryModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.model.LedgerRecordModel;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.HInventoryMaterielInfoService;
import com.zhongwang.mms.module.half.service.HInventoryService;
import com.zhongwang.mms.module.half.service.InvInfoEditService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:库存信息编辑
 * @author: th
 * @time: 2020/4/8 10:17
 */
@Controller
@RequestMapping("/half/halInvManage/InvInfoEdit")
public class InvInfoEditController extends BaseController {

    //页面文件路径
    private String prefix = "half/halInvManage/InvInfoEdit";

    @Autowired
    private InvInfoEditService invInfoEditService;
    @Autowired
    private HInventoryService hInventoryService;
    @Autowired
    private HInventoryMaterielInfoService hInventoryMaterielInfoService;
    @Autowired
    private HAnnexDao hAnnexDao;
    @Autowired
    private HBasicService hBasicService;

    // 页面
    @GetMapping("/page")
    @RequiresPermissions({"halinv:halinvedit:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
        return prefix + "/list";
    }

    // 库存列表
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"halinv:halinvedit:view"})
    public JsonResult stockList(HInventoryQueryModel query){

        startPage();
        return jsonResult(invInfoEditService.selectInventoryByQuery(query));
    }

    // 弹出框数据
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"halinv:halinvedit:view"})
    public JsonResult editInv(Integer ityId){

        return jsonResult(hInventoryService.selectInvByItyId(ityId));
    }

    // 提交弹出框数据
    @ResponseBody
    @PostMapping("/invCommit")
    @RequiresPermissions({"halinv:halinvedit:edit"})
    public JsonResult invCommit(HInventoryModel hInventoryModel){

        return jsonResult(hInventoryService.updateInvByItyId(hInventoryModel));
    }

    // 台账查询
    @GetMapping("/ledgerQuery")
    @RequiresPermissions({"halinv:halledgeredit:view"})
    public String ledgerQuery(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("warInfo", hBasicService.getWarInfo());
        return prefix + "/ledgerQuery";
    }

    // 台账查询加载数据
    @ResponseBody
    @PostMapping("/ledgerQueryList")
    @RequiresPermissions({"halinv:halledgeredit:view"})
    public JsonResult ledgerQueryList(LedgerRecordModel ledgerRecordModel){

        startPage();
        return jsonResult(hInventoryMaterielInfoService.selectAllByQuery(ledgerRecordModel));
    }

    //记录台账
    @GetMapping("/ledger/{ityId}")
    @RequiresPermissions({"halinv:halledgeredit:view"})
    public String viewOutBill(@PathVariable("ityId") Integer ityId, Model model) {

        List<HAnnex> hAnnex = hAnnexDao.selectAnnexByItyId(ityId);
        model.addAttribute("hAnnex", hAnnex);
        model.addAttribute("imiItyId", ityId);

        return prefix + "/ledger";
    }

    //记录台账加载数据
    @ResponseBody
    @PostMapping("/ledgerList/{ityId}")
    @RequiresPermissions({"halinv:halledgeredit:view"})
    public JsonResult ledgerList(@PathVariable("ityId")Integer ityId, Model model){

        return jsonResult(hInventoryMaterielInfoService.selectAllByItyId(ityId));
    }

    //新增台账数据
    @ResponseBody
    @PostMapping("/ledgerCommit")
    @RequiresPermissions({"halinv:halledgeredit:edit"})
    public JsonResult ledgerCommit(LedgerRecordModel ledgerRecordModel){

        return jsonResult(hInventoryMaterielInfoService.insertLedgerByItyId(ledgerRecordModel));
    }

    //删除台账数据
    @ResponseBody
    @PostMapping("/deleteLedger")
    @RequiresPermissions({"halinv:halledgeredit:edit"})
    public JsonResult deleteLedger(Integer imiId){

        return jsonResult(hInventoryMaterielInfoService.deleteLedgerByItyId(imiId));
    }

    /**
     * 查看台账
     * @param imiItyId
     * @return
     */
    @ResponseBody
    @PostMapping("/view")
    @RequiresPermissions({"halinv:halledgeredit:view"})
    public JsonResult view(Integer imiItyId){

        startPage();
        return jsonResult(hInventoryMaterielInfoService.selectAllByItyId(imiItyId));
    }



    //上传台账文件
    @ResponseBody
    @PostMapping("/uploadFile")
    @RequiresPermissions({"halinv:halledgeredit:edit"})
    public JsonResult uploadFile(@RequestParam("file") MultipartFile[] file,@RequestParam("ityId") Integer ityId){

        return jsonResult(hInventoryMaterielInfoService.uploadFile(file,ityId));
    }

    @ResponseBody
    @PostMapping("/delFile")
    @RequiresPermissions({"halinv:halledgeredit:edit"})
    public JsonResult uploadFile(Integer anxId){

        return jsonResult(hInventoryMaterielInfoService.delFile(anxId));
    }

    /**
     * 库存信息批量修改
     * @param param
     * @return
     */
    @PostMapping("/invCommits")
    @ResponseBody
    @RequiresPermissions({"halinv:halinvedit:edit"})
    public JsonResult save(HInventoryModel param) {
        boolean result = hInventoryService.invCommits(param);
        if (!result) {
            return JsonResult.fail("修改失败！");
        }
        return JsonResult.success("修改成功！");
    }
}
